﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Vboot.App.K3CloudDataSend
{
    public class K3CloudDataRequest<T>
    {

        /// <summary>
        /// 一张单保存
        /// </summary>
        /// <param name="baseSaveEntity"></param>
        /// <param name="formId"></param>
        /// <returns></returns>
        public static async Task<JObject> K3SaveBillRequest(T baseSaveEntity, string formId)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(K3CloudLogin.CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");
            JObject saveJobj = JObject.FromObject(baseSaveEntity);
            List<object> Parameters = new List<object>();
            Parameters.Add(formId);
            Parameters.Add(saveJobj.ToString());
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            string ResultsStr = httpClient.AsyncRequest();
            JObject ResultObj = JObject.Parse(ResultsStr);


            return ResultObj;
        }


        /// <summary>
        /// 批量保存
        /// </summary>
        /// <param name="model"></param>
        /// <param name="formId"></param>
        /// <returns></returns>
        public static async Task<JObject> K3BatchSaveBillRequest(T baseSaveEntity, string formId)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(K3CloudLogin.CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc");
            JObject saveJobj = JObject.FromObject(baseSaveEntity);
            List<object> Parameters = new List<object>();
            Parameters.Add(formId);
            Parameters.Add(saveJobj.ToString());
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            string ResultsStr = httpClient.AsyncRequest();
            JObject ResultObj = JObject.Parse(ResultsStr);

            return ResultObj;
        }

        ///// <summary>
        ///// 金蝶单据查询api(返回查询结果)
        ///// </summary>
        ///// <param name="formid">查询表单元数据唯一标识</param>
        ///// <param name="toprowcount">最多允许查询的数量，0或者不要此属性表示不限制</param>
        ///// <param name="limit">分页取数每页允许获取的数据，最大不能超过2000</param>
        ///// <param name="startrow">分页取数开始行索引，从0开始，例如每页10行数据，第2页开始是10，第3页开始是20</param>
        ///// <param name="orderstring">过滤条件</param>
        ///// <param name="condiStr">排序条件</param>
        ///// <param name="cols">待查询表单的字段列表</param>
        ///// <returns></returns>
        //public static JArray K3BillDataQueryRequest(string formid, int toprowcount, int limit, int startrow, string orderstring, string condiStr, string cols)
        //{
        //    List<object> Parameters = new List<object>();

        //    K3CloudLogin k3CloudLogin = new K3CloudLogin();
        //    k3CloudLogin.CheckLogin();

        //    HttpClient httpClient = new HttpClient();
        //    httpClient.Url = string.Concat(K3CloudLogin.CloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc");

        //    string data = "{\"FormId\":\"" + formid + "\",\"TopRowCount\":" + toprowcount + ",\"Limit\":" + limit + ",\"StartRow\":" + startrow + "," +
        //                          "\"FilterString\":\"" + condiStr + "\",\"OrderString\":\"" + orderstring + "\"," + "\"FieldKeys\":\"" + cols + "\"}";
        //    Parameters.Add(data);
        //    httpClient.Content = JsonConvert.SerializeObject(Parameters);
        //    var results = httpClient.AsyncRequest();

        //    JArray jArray = JArray.Parse(results);

        //    if (!results.ToString().Contains("\"Errors\""))
        //    {
        //        return jArray;
        //    }
        //    else
        //    {
        //        UIMessageBox.ShowError("无法获取金蝶数据！");
        //        throw new Exception(jArray[0].ToString());
        //    }

        //}
    }
}
